package com.bite.book.service;

//import com.bite.book.dao.BookDao;
import com.bite.book.enums.BookStatusEnum;
import com.bite.book.mapper.BookMapper;
import com.bite.book.model.BookInfo;
import com.bite.book.model.PageRequest;
import com.bite.book.model.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
//    @Autowired
//    private BookDao bookDao;

    @Autowired
    private BookMapper bookMapper;
//    public List<BookInfo> getList(){
//
//        List<BookInfo> bookInfos = bookDao.mockData();
//        //对结果进行二次处理
//        for (BookInfo bookInfo: bookInfos){
//            if (bookInfo.getStatus()==1){
//                bookInfo.setStatusCN("可借阅");
//            }else {
//                bookInfo.setStatusCN("不可借阅");
//            }
//        }
//        return bookInfos;
//    }

    public void addBook(BookInfo bookInfo) {
        bookMapper.addBook(bookInfo);
    }
    public ResponseResult<BookInfo> getListByPage(PageRequest pageRequest){

        // 1. 获得总图书数
        //2. 获取当前页的数据
        Integer count = bookMapper.count();
        List<BookInfo> bookInfos = bookMapper.selectBooksByPage(pageRequest);
        //对结果进行二次处理
        for (BookInfo bookInfo: bookInfos){
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
        }
        return new ResponseResult<>(count, bookInfos, pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    public void updateBook(BookInfo bookInfo) {
        bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids){
        return bookMapper.batchDelete(ids);
    }
}
